<?php

@header("Content-type:text/html; charset=UTF-8");       
@header("Cache-Control: no-store, no-cache, must-revalidate");      
@header("Cache-Control: post-check=0, pre-check=0", false); 

require '../Controller/Config.php';
require_once '../Controller/ConnectFB.php';


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
<head>
<title><?php echo $_GET['poll'] ?></title>
<link rel="image_src" href="../images/LogoShared2.png" />
</head>
<style>
    body{
    	background-image:url('../images/bg.jpg');
        background-repeat:repeat-x;
        background-color:#f8e3b7;
    }
</style>
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Loader" type="text/javascript"></script>
<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
<?php

$arrAnsVal = array();


$userId = $fb_userData['id'];
//$userId = 1008;

//Get Answer Value
//$a = 0;
//$sql_ans_val = "SELECT * FROM answer WHERE user_id = '".$userId."'";
//$rs = mysql_query($sql_ans_val) or die("Error SQL[ ".$sql_ans_val." ] ".mysql_error());
//while ($row = mysql_fetch_array($rs)) {
//    $arrAnsVal[$a][$row['poll_id']][$row['question_id']] = $row['answer_value'];
//    $a++;
//}
//mysql_free_result($rs);
//
//echo "<pre>";
//echo print_r($arrAnsVal);
//echo "</pre>";

//Count answer
$sql_check_a = "SELECT COUNT(*) AS count FROM poll WHERE created_by = '".$userId."'";
$rs_ss_a = mysql_query($sql_check_a) or die("Error SQL[ ".$sql_check_a." ] ".mysql_error());
while ($row = mysql_fetch_array($rs_ss_a)) {
    $numrs_a = $row['count'];
}
mysql_free_result($rs_ss_a);

//Count answer
$sql_check_b = "SELECT COUNT(*) AS count FROM answer WHERE user_id = '".$userId."'";
$rs_ss_b = mysql_query($sql_check_b) or die("Error SQL[ ".$sql_check_b." ] ".mysql_error());
while ($row = mysql_fetch_array($rs_ss_b)) {
    $numrs_b = $row['count'];
}
mysql_free_result($rs_ss_b);

$save_disable = "";
if(($numrs_a == 0) && ($numrs_b == 0)){
    $save_disable = "";
    @header("Location: ../View/CheckCondition.php?pollId=".$_GET['pollId']."&poll=".$_GET['poll']."");
}else{
    $save_disable = "disabled";
}

if(isset($userId) && isset($_GET['pollId'])){
    if(($userId!=NULL && $userId!="") || ($_GET['pollId']!=NULL && $_GET['pollId']!="")){

        $pollId = $_GET['pollId'];

        $query_poll = "SELECT * FROM poll WHERE poll_id = ".$pollId;
        $rs1 = mysql_query($query_poll) or die("Error SQL[ ".$query_poll." ] ".mysql_error());

        echo <<<HTML
            <form name="pollFrm" id="pollFrm" method="post" action="../Controller/SaveAnswerAction.php" >
HTML;

        $i=0;
        $inputType = "";
        $arrData = array();
        $arrScale = array();

        //fetch poll by created_by AND poll_id
        while($obj1 = mysql_fetch_array($rs1)){

            echo "<b>ชื่อแบบสอบถาม : ".$obj1['poll_title']."</b>";
            echo "<br />";
            echo "<b>รายละเอียดเกียวกับแบบสอบถาม : ".$obj1['poll_desc']."</b>";
            $pollDetial = $obj1['poll_desc'];
            echo "<br /><br />";  
            $pollDetial = $obj1['poll_desc'];
            $arrData['poll_title'] = $obj1['poll_title'];
            $arrData['poll_desc'] = $obj1['poll_desc'];

            $query_question = "SELECT * FROM question WHERE poll_id = ".$obj1['poll_id'];
            $rs2 = mysql_query($query_question) or die("Error SQL[ ".$query_question." ] ".mysql_error());

            //fetch question by poll_id
            while($obj2 = mysql_fetch_array($rs2)){
                
                
                if($obj2['question_type']==1){                  
                    $inputType = '<input type="radio" name="data'.$i.'[]" />';
                }else if($obj2['question_type']==2){
                    $inputType = '<input type="checkbox" name="data'.$i.'[]" />';
                }else if($obj2['question_type']==3){
                    $inputType = '<textarea cols="30" rows="5" name="data'.$i.'[]" ></textarea>';
                }else if($obj2['question_type']==4){
                    $inputType = 'scale';
                    $inputTypeScale = '';
                }
                
                echo '<input type="hidden" name="questionId[]" value="'.$obj2['question_id'].'" />';
                echo "<b>".($i+1).". ".$obj2['question_value']."</b>";
                echo "<br />";            


                $query_choice = "SELECT * FROM choice WHERE question_id = ".$obj2['question_id']." AND poll_id = ".$obj1['poll_id'];
                $rs3 = mysql_query($query_choice) or die("Error SQL[ ".$query_choice." ] ".mysql_error());
                $j=0;
                $k=0;
                $n=0;
                $m=0;
                $arrScale = array();
                $arrScaleLabel = array();

                //fetch choice by question_id AND poll_id
                while($obj3 = mysql_fetch_array($rs3)){
                    $a=0;  
                    $sql_ans_val = "SELECT * FROM answer WHERE user_id = '".$userId."' AND question_id = ".$obj2['question_id']."";
                    $rs = mysql_query($sql_ans_val) or die("Error SQL[ ".$sql_ans_val." ] ".mysql_error());
                    while ($row = mysql_fetch_array($rs)) {
                        $arrAnsVal[$m][$obj2['question_id']][$a] = $row['answer_value'];
                        $a++;
                    }
                    
                   
                    if($inputType!="scale"){
                        if($obj2['question_type']==3){
                            $inputType = '<textarea cols="30" rows="5" disabled name="data'.$i.'[]" >'.$arrAnsVal[$m][$obj2['question_id']][$k].'</textarea>';
                            echo $inputType;
                            echo "<br />";
                            
                            $arrData[$i][$obj2['question_value']][$k] = $inputType;
                            $k++;
                        }else{

                                if($obj2['question_type']==1){
                                        $value = ($arrAnsVal[$m][$obj2['question_id']][0]==$obj3['choice_value']) ? "checked" : "";
                                        $inputType = '<input disabled type="radio" name="data'.$i.'[]" '.$value.' />';
                                }else if($obj2['question_type']==2){
                                        
                                        for($q=0;$q<count($arrAnsVal[$m][$obj2['question_id']]);$q++){
                                            if($arrAnsVal[$m][$obj2['question_id']][$q]==$obj3['choice_value']){
                                                $inputType = '<input disabled type="checkbox" name="data'.$i.'[]" checked />';
                                                $q++;
                                            }else{
                                                $inputType = '<input disabled type="checkbox" name="data'.$i.'[]"  />';
                                            }
                                            
                                        }
                                            
                                        
                                        
                                }

                            echo $inputType." ".$obj3['choice_value'];
                            echo "<br />";

                            $arrData[$i][$obj2['question_value']][$k] = $inputType." ".$obj3['choice_value'];
                            $k++;
                        }
                    }else{

                        $arrScale[$j] = $obj3['choice_value'];


                        $query_option = "SELECT * FROM option_scale WHERE choice_id = ".$obj3['choice_id'];
                        $rs4 = mysql_query($query_option) or die("Error SQL[ ".$query_option." ] ".mysql_error());

                        //fetch option_scale by choice_id
                        while($obj4 = mysql_fetch_array($rs4)){

                                $arrScaleLabel[$j] = $obj4['option_value'];

                                $arrData[$i][$obj2['question_value']][$obj3['choice_value']] = $obj4['option_value'];

                        }

                        if($j==1){

                                for($loop = $arrScale[0]; $loop <= $arrScale[1]; $loop++){
                                        if($loop==$arrAnsVal[$m][$obj2['question_id']][0]){
                                            $inputTypeScale .= '<input disabled type="radio" name="data'.$i.'[]" checked />';
                                        }else{
                                            $inputTypeScale .= '<input disabled type="radio" name="data'.$i.'[]"  />';
                                        }
                                        
                                }
                                echo $arrScaleLabel[0]." [".$arrScale[0]."] ".$inputTypeScale." [".$arrScale[1]."] ".$arrScaleLabel[1];
                                echo "<br />";
                        }

                        $j++;
                    }
                    $m++;
                }
                echo "<br />";
                $i++;
            }    
        }
        mysql_close();
        echo "<br />";
        $title=urlencode($_GET['poll']);
        $url=urlencode("http://".$_SERVER['HTTP_HOST']."/project/Interface/MenuAnswer.php?page=CheckCondition&pollId=".$_GET['pollId']."&poll=".$_GET['poll']);
        $summary=urlencode($pollDetial);
        $image=urlencode($_SERVER['HTTP_HOST']."/project/images/headlogo.png");

//        $pathLink = $_SERVER['HTTP_HOST']."/project/Interface/MenuAnswer.php?page=ViewAnswer&pollId=".$_GET['pollId']."&poll=".$_GET['poll'];
        
        echo <<<HTML
            <a onClick="window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]=$title&amp;p[summary]=$summary&amp;p[url]=$url&amp;&amp;p[images][0]=$image','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)"><image src="../icon/facebook.png" style="width:80px"></a>    
            </form>
HTML;


//         echo "<pre>";
//         print_r($arrAnsVal);
//         echo "</pre>";

    }
}else{
    echo "Please login to access this";
}
?> 